package cn.itsource.serivce.impl;

import cn.itsource.domain.Employee;
import cn.itsource.domain.Permission;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.mapper.PermissionMapper;
import cn.itsource.serivce.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper ;

    @Autowired
    private PermissionMapper permissionMapper ;

    /**--------------------------------------------------------
    登录
     1.判断参数
     2.根据用户名查询Employee
     3.比对密码
     4.把登录用户存储到Session
     --------------------------------------------------------**/
    @Override
    public Employee login(String username, String password) {
        //1.判断参数
        if(!StringUtils.hasLength(username)){
            throw new RuntimeException("用户名不可为空");
        }

        if(!StringUtils.hasLength(password)){
            throw new RuntimeException("密码不可为空");
        }

        //2.根据用户名查询Employee
        Employee employee = employeeMapper.selectByUsername(username);

        //3.比对密码
        if(null == employee){
            throw new RuntimeException("用户名不存在");
        }

        if(!employee.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }
        //查询用户的权限列表
        List<Permission> permissions =  permissionMapper.selectByUserId(employee.getId());
        employee.setPermissions(permissions);

        //4.把登录用户存储到Session

        return employee;
    }
}
